package spider

import (
	"gdei_healthy_colly/config"
	"net/http"
	"net/http/cookiejar"
)

// 账号
type Account interface {
	GetId() string
	GetUsername() string
	GetPassword() string
	GetRemember() bool
	GetClient() *http.Client
}

// 配置文件实现
func LoadAccounts() (acs []*configAccount) {
	acs = make([]*configAccount, 0, len(config.Cfg.Accounts))
	for _, v := range config.Cfg.Accounts {
		jar, _ := cookiejar.New(nil)

		acs = append(acs, &configAccount{
			ID:       v.ID,
			Username: v.Username,
			Password: v.Password,
			Remember: v.Remember,
			client: &http.Client{
				Jar: jar,
			},
		})
	}
	return
}

// 配置文件账号
type configAccount struct {
	ID       string
	Username string
	Password string
	Remember bool
	client   *http.Client
}

var _ Account = (*configAccount)(nil)

func (ac *configAccount) GetId() string {
	return ac.ID
}

func (ac *configAccount) GetUsername() string {
	return ac.Username
}

func (ac *configAccount) GetPassword() string {
	return ac.Password
}

func (ac *configAccount) GetRemember() bool {
	return ac.Remember
}

func (ac *configAccount) GetClient() *http.Client {
	if ac.client == nil {
		jar, _ := cookiejar.New(nil)
		ac.client = &http.Client{
			Jar: jar,
		}
	}
	return ac.client
}
